// pages/addMembers/addMembers.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Headerimage: "https://shunhui.at1984.com:8443/resource/userfiles/shunhui/shunhui_user_head.png",
    drivername: "",
    driverphone: "",
    hamalname: "",
    hamalphone: "",
    isshowadd: false,
    isshowmodify: false,
    isshowtime: false,
    workTime: "",
    designerId: "",
    customername: "",
    customerphone: "",
    designername: "",
    designerphone: "",
    // 司机车牌号
    licensePlate:""
  },
  zhanshi(){},
  // 添加设计师
  saveDesigner() {
    let t = this
    wx.showLoading({
      title: '加载中',
      
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'saveDesigner',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        name: t.data.designername,
        designerId: t.data.designerId,
        token: t.data.token,
        mobile: t.data.designerphone,
        beginWorkDate: t.data.workTime
      },
      success: res => {
        console.log(res)
        wx.hideLoading()
        if (res.data.result == 0) {
          if (t.data.designerId == '') {
            this.setData({
              isshowadd: true
            })
          } else {
            this.setData({
              isshowmodify: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
          t.setData({
            token:app.token()
          })
        }

      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },


  add() {
    // 添加设计师
    if (this.data.todo == 'adddesigner') {
      if (this.data.designername == '') {
        wx.showToast({
          title: '请输入设计师姓名',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }
      if (this.data.designerphone == '') {
        wx.showToast({
          title: '请输入设计师手机号',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      if (this.data.workTime == '') {
        wx.showToast({
          title: '请输入设计师从业时间',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }

      this.saveDesigner()

    }
  },
  bindDateworkChange(e) {
    this.setData({
      workTime: e.detail.value
    })
  },
  // 是否显示遮罩层
  HidesuccessaddMask() {
    this.setData({
      isshowadd: false
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getDesignerList()
    wx.navigateBack({
      delta: 1,
    })
  },
  HidesuccessmodifyMask() {
   if(this.data.todo=='modifyOrderer'){
    this.setData({
      isshowmodify: false
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getOrderDetails()
    wx.navigateBack({
      delta: 1,
    })
   }else{
    this.setData({
      isshowmodify: false
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getDesignerList()
    wx.navigateBack({
      delta: 1,
    })
   }
  },
  HidesuccessMask() {
    this.setData({
      isshow: false
    })

  },


  // 判断手机号码是否正确
  bindPlone(e) {

    let phone = e.detail.value
    let phoneCodeVerification = /^[1][0-9]{10}$/;
    if (!phoneCodeVerification.test(phone)) {
      wx.showToast({
        title: '您输入手机号码输入不正确',
        duration: 2500, //持续的时间
            icon: 'none', //图标
      })
      this.setData({
        driverphone: "",
        customerphone: "",
        designerphone: "",
        hamalphone: "",
      })
    } else {

    }

  },
  next() {

    //  店长跳转到房产信息
    if (this.data.todo == 'addcustomer') {

      if (this.data.customername == '') {
        wx.showToast({
          title: '客户姓名不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      if (this.data.customerphone == "") {
        wx.showToast({
          title: '客户电话号码不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      let orderObj = {}
      orderObj.customerName = this.data.customername,
        orderObj.customerPhone = this.data.customerphone
      wx.navigateTo({
        url: '/shopowner/pages/addProperty/addProperty?orderObj=' + JSON.stringify(orderObj),
      })
    } else if (this.data.todo == 'adddriver') {

      if(this.data.drivername==''){
        wx.showToast({
          title: '请填写司机姓名',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      
      if(this.data.driverphone==''){
        wx.showToast({
          title: '请填写司机电话',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      if(this.data.licensePlate==''){
        wx.showToast({
          title: '请输入车牌号',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;

      if(!express.test(this.data.licensePlate)){
        wx.showToast({
          title: '请输入正确的车牌号',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        return
      }
      // 跳转添加搬运工
      let orderObj = this.data.orderObj
      orderObj.driverName = this.data.drivername
      orderObj.driverMobile = this.data.driverphone
      orderObj.licensePlate = this.data.licensePlate
      wx.navigateTo({
        url: '/shopowner/pages/addpeople/addpeople?todo=hamal&&orderObj=' + JSON.stringify(orderObj) + '&&orderId=' + this.data.orderId,
      })
    } else if (this.data.todo == 'hamal' || this.data.todo == 'modifyhamal') {
      if(this.data.hamalname==''){
        wx.showToast({
          title: '请填写搬运工姓名',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }
      
      if(this.data.hamalphone==''){
        wx.showToast({
          title: '请填写搬运工电话',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        return
      }
      this.setData({
        isshowtime: true
      })
    }





  },
  // 获得发车时间
  bindstatedate(e) {
    console.log(e)
    this.setData({
      stateDate: e.detail.value,
      endDate: e.detail.value,
    })
  },
  bindstatetime(e) {
    this.setData({
      statetime: e.detail.value,
      endTiem: e.detail.value
    })
  },
  // 送达时间
  bindenddate(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  bindendtime(e) {
    this.setData({
      endTiem: e.detail.value
    })
  },



  // 店长创建编辑项目
  saveProjectOrder() {
    let t = this
    wx.showLoading({
      title: '加载中',
      
      mask: true //显示透明蒙层 防止触摸穿透
    })
    console.log("执行4")
    wx.request({
      url: app.globalData.Interfaceurl + 'saveProjectOrder',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: "1569562832576524288",
        token: t.data.token,
        orderId: t.data.orderObj.orderId,
        customerName: t.data.orderObj.customerName,
        customerPhone: t.data.orderObj.customerPhone,
      },
      success: res => {
        wx.hideLoading()
      if(res.data.result==0){
        t.setData({
          isshowmodify: true
        })
      }else{
        wx.showToast({
          title: res.data.message,
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        t.setData({
          token: app.token()
        })
      }
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },



  determine() {
    let orderObj = this.data.orderObj;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    if (this.data.todo == 'modifyOrder') {
      if (this.data.customername == '') {
        wx.showToast({
          title: '客户姓名不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        this.setData({
          isshowtime:false
        })
        return
      }
      if (this.data.customerphone == "") {
        wx.showToast({
          title: '客户电话号码不能为空',
          duration: 2500, //持续的时间
            icon: 'none', //图标
        })
        this.setData({
          isshowtime:false
        })
        return
      }
      orderObj.customerName = this.data.customername
      orderObj.customerPhone = this.data.customerphone
 
      prevPage.setData({
        orderObj: orderObj
      })
      wx.navigateBack({
        delta: 1,
      })

    } else if(this.data.todo == 'modifyOrderer'){
      if (this.data.customername == '') {
        wx.showToast({
          title: '客户姓名不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        this.setData({
          isshowtime:false
        })
        return
      }
      if (this.data.customerphone == "") {
        wx.showToast({
          title: '客户电话号码不能为空',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
        this.setData({
          isshowtime:false
        })
        return
      }
      orderObj.customerName = this.data.customername
      orderObj.customerPhone = this.data.customerphone
      this.saveProjectOrder()



    }
    
    
    
    
    else if (this.data.todo == 'modifydrivery') {
      orderObj.licensePlate = this.data.licensePlate
      orderObj.driverName = this.data.drivername
      orderObj.driverMobile = this.data.driverphone
      prevPage.setData({
        orderObj: orderObj
      })

      wx.navigateBack({
        delta: 1,
      })
    } else if (this.data.todo == 'modifyhamal') {
      let orderObj = this.data.orderObj
      orderObj.workerName = this.data.hamalname
      orderObj.workerMobile = this.data.hamalphone
      orderObj.leaveDate = this.data.stateDate + " " + this.data.statetime
      orderObj.serviceDate = this.data.endDate + " " + this.data.endTiem
      orderObj.stateDate=this.data.stateDate
      orderObj.statetime=this.data.statetime
      orderObj.endDate=this.data.endDate
      orderObj.endTiem=this.data.endTiem
      prevPage.setData({
        orderObj: orderObj
      })

      wx.navigateBack({
        delta: 1,
      })
    }
  },
  hidetime(){
    this.setData({
      isshowtime:false
    })
  },
  preview() {
    if(this.data.hamalname==''){
      wx.showToast({
        title: '请填写搬运工姓名',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }
    
    if(this.data.hamalphone==''){
      wx.showToast({
        title: '请填写搬运工姓名',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }

    let orderObj = this.data.orderObj
    orderObj.workerName = this.data.hamalname
    orderObj.workerMobile = this.data.hamalphone
    orderObj.leaveDate = this.data.stateDate + " " + this.data.statetime
    orderObj.serviceDate = this.data.endDate + " " + this.data.endTiem
    orderObj.stateDate=this.data.stateDate
    orderObj.statetime=this.data.statetime
    orderObj.endDate=this.data.endDate
    orderObj.endTiem=this.data.endTiem
    
    // console.log(orderObj)
    // return
    wx.navigateTo({
      url: '/shopowner/pages/previewOrder/previewOrder?todo=Delivery&&orderObj=' + JSON.stringify(this.data.orderObj) + '&&orderId=' + this.data.orderId,
    })
  },
  fakeCallback() {},
  // 获得设计师详情
  getDesignerDetails(designerid) {
    let t = this
    wx.showLoading({
      title: '加载中...', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getDesignerDetails',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        designerId: designerid,
      },
      success: res => {
        console.log(res)
        wx.hideLoading();
        let designer = res.data.designer
        this.setData({
          designername: designer.name,
          designerphone: designer.mobile,
          workTime: designer.beginWorkDate,
          designerId: designer.id,
          Headerimage: designer.image
        })
      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    if (options.todo) {
      this.setData({
        todo: options.todo,
        token: app.globalData.token
      })
    }

    if(options.orderObj){
      var orderObj = JSON.parse(options.orderObj)
      this.setData({
        orderObj: orderObj,
      })
    }   

    if (options.todo == "modifyOrder"||options.todo == "modifyOrderer") {
      this.setData({
        customername: orderObj.customerName,
        customerphone: orderObj.customerPhone,
        token:app.token()
      })
    }
    if (this.data.todo == 'hamal') {
      let date = new Date()
      let year = date.getFullYear()
      let mouth = date.getMonth() + 1
      mouth = mouth < 10 ? '0' + mouth : mouth
      let data = date.getDate()
      data = data < 10 ? '0' + data : data
      let h = date.getHours()
      h = h < 10 ? '0' + h : h
      let m = date.getMinutes()
      m = m < 10 ? '0' + m : m
      this.setData({
        stateDate: year + "-" + mouth + "-" + data,
        statetime: h + ":" + m,
        endDate: year + "-" + mouth + "-" + data,
        endTiem: h + ":" + m,
        nowdate: year + "-" + mouth + "-" + data,
        nowtime: h + ":" + m,
      })
    }

  
    // 添加房产信息
    // 设计师
    if (this.data.todo == 'adddesigner') {
      let date = new Date()
      let year = date.getFullYear()
      let mouth = date.getMonth() + 1
      mouth = mouth < 10 ? '0' + mouth : mouth
      let data = date.getDate()
      data = data < 10 ? '0' + data : data
      let h = date.getHours()
      h = h < 10 ? '0' + h : h
      let m = date.getMinutes()
      m = m < 10 ? '0' + m : m
      this.setData({
        token: app.token(),
        nowdate: year + "-" + mouth + "-" + data,
      })
      if (options.designerid) {
        this.getDesignerDetails(options.designerid)
      }
    }
    // 添加客户信息
    if (this.data.todo == 'addcustomer') {
     
    }
    // 添加司机信息
    if (this.data.todo == 'adddriver') {
      this.setData({
        orderId: options.orderId
      })
    }
    if (this.data.todo == 'modifydrivery') {
      this.setData({
        drivername: orderObj.driverName,
        driverphone:orderObj.driverMobile,
      })

    }


    // 添加搬运工
    if (this.data.todo == 'hamal') {
      this.setData({
        orderObj: JSON.parse(options.orderObj),
        orderId: options.orderId
      })
    }
    if (this.data.todo == 'modifyhamal') {
      let date = new Date()
      let year = date.getFullYear()
      let mouth = date.getMonth() + 1
      mouth = mouth < 10 ? '0' + mouth : mouth
      let data = date.getDate()
      data = data < 10 ? '0' + data : data
      let h = date.getHours()
      h = h < 10 ? '0' + h : h
      let m = date.getMinutes()
      m = m < 10 ? '0' + m : m
      this.setData({
        hamalname: orderObj.workerName,
        hamalphone: orderObj.workerMobile,
        stateDate: orderObj.stateDate,
        statetime: orderObj.statetime,
        endDate: orderObj.endDate,
        endTiem: orderObj.endTiem,
        nowdate:year+mouth+data,
        nowtime: h+m,
      })

    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})